from otree.api import Currency as c, currency_range
from ._builtin import Page, WaitPage
from .models import Constants


# form_model = 'player'
# form_fields = ['game_point']
#
# def before_next_page(self):
#     player = self.player
#     timeout_happened = self.timeout_happened
#     if timeout_happened:
#         # 这里是值 可以随机加入 模仿机器人
#         player.point=1
#         self.participant.vars['is_dropout'] = True


class ResultsWaitPage1(WaitPage):
    after_all_players_arrive = 'set_initial_CalculateGamePoint'
    template_name = 'testgame/ResultsWaitPage.html'
    title_text = "等待页面"
    body_text = "请耐心等待其他同学完成任务。"


class ResultsWaitPage2(WaitPage):
    template_name = 'testgame/ResultsWaitPage.html'
    title_text = "等待页面"
    body_text = "请耐心等待其他同学完成任务。"


class ResultsWaitPage3(WaitPage):
    template_name = 'testgame/ResultsWaitPage.html'
    title_text = "等待页面"
    body_text = "请耐心等待其他同学完成任务。"


class ResultsWaitPage4(WaitPage):
    after_all_players_arrive = 'set_payoffs'
    template_name = 'testgame/ResultsWaitPage.html'
    title_text = "等待页面"
    body_text = "请耐心等待其他同学完成任务。"


class SliderGameIntroduce(Page):
    pass


class CalculateGameIntroduce(Page):
    pass


class Congratulations(Page):
    pass


class ResultsAndCalculateGameIntroduce(Page):
    timer_text = '倒计时结束自动跳转:'
    timeout_seconds = 40


class SliderGame(Page):
    pass


class SliderGameTest(Page):
    timer_text = '剩余的试玩时间:'
    timeout_seconds = 60


class CalculateGame1(Page):
    form_model = 'player'
    form_fields = ['answer_1', 'answer_2', 'answer_3', 'answer_4', 'answer_5', 'answer_6',
                   'answer_7', 'answer_8', 'answer_9', 'answer_10', 'answer_11', 'answer_12', 'answer_13', 'answer_14',
                   'answer_15', 'answer_16', 'answer_17', 'answer_18',
                   'answer_19', 'answer_20', ]

    # 1-12题 按照1-36随机数来依次计算
    # 顺序是横着数的 页面上第一列是1和2 第二列是3和4 以此类推
    def before_next_page(self):
        Subsession = self.subsession
        Group = self.group
        Player = self.player
        # 如果提交为空 则改为提交0即可
        if Player.answer_1 == None:
            Player.answer_1 = 0
        if Player.answer_2 == None:
            Player.answer_2 = 0
        if Player.answer_3 == None:
            Player.answer_3 = 0
        if Player.answer_4 == None:
            Player.answer_4 = 0
        if Player.answer_5 == None:
            Player.answer_5 = 0
        if Player.answer_6 == None:
            Player.answer_6 = 0
        if Player.answer_7 == None:
            Player.answer_7 = 0
        if Player.answer_8 == None:
            Player.answer_8 = 0
        if Player.answer_9 == None:
            Player.answer_9 = 0
        if Player.answer_10 == None:
            Player.answer_10 = 0
        if Player.answer_11 == None:
            Player.answer_11 = 0
        if Player.answer_12 == None:
            Player.answer_12 = 0
        if Player.answer_13 == None:
            Player.answer_13 = 0
        if Player.answer_14 == None:
            Player.answer_14 = 0
        if Player.answer_15 == None:
            Player.answer_15 = 0
        if Player.answer_16 == None:
            Player.answer_16 = 0
        if Player.answer_17 == None:
            Player.answer_17 = 0
        if Player.answer_18 == None:
            Player.answer_18 = 0
        if Player.answer_19 == None:
            Player.answer_19 = 0
        if Player.answer_20 == None:
            Player.answer_20 = 0

        # 计算正确个数 并加分
        right = 0
        if Player.answer_1 == Subsession.RandomNumber_1 + Subsession.RandomNumber_2:
            right = right + 1
        if Player.answer_2 == Subsession.RandomNumber_3 + Subsession.RandomNumber_4:
            right = right + 1
        if Player.answer_3 == Subsession.RandomNumber_5 + Subsession.RandomNumber_6:
            right = right + 1
        if Player.answer_4 == Subsession.RandomNumber_7 + Subsession.RandomNumber_8:
            right = right + 1
        if Player.answer_5 == Subsession.RandomNumber_9 + Subsession.RandomNumber_10:
            right = right + 1
        if Player.answer_6 == Subsession.RandomNumber_11 + Subsession.RandomNumber_12:
            right = right + 1
        if Player.answer_7 == Subsession.RandomNumber_13 + Subsession.RandomNumber_14:
            right = right + 1
        if Player.answer_8 == Subsession.RandomNumber_15 + Subsession.RandomNumber_16:
            right = right + 1
        if Player.answer_9 == Subsession.RandomNumber_17 + Subsession.RandomNumber_18:
            right = right + 1
        if Player.answer_10 == Subsession.RandomNumber_19 + Subsession.RandomNumber_20:
            right = right + 1
        if Player.answer_11 == Subsession.RandomNumber_21 + Subsession.RandomNumber_22:
            right = right + 1
        if Player.answer_12 == Subsession.RandomNumber_23 + Subsession.RandomNumber_24:
            right = right + 1
        if Player.answer_13 == Subsession.RandomNumber_25 + Subsession.RandomNumber_26:
            right = right + 1
        if Player.answer_14 == Subsession.RandomNumber_27 + Subsession.RandomNumber_28:
            right = right + 1
        if Player.answer_15 == Subsession.RandomNumber_29 + Subsession.RandomNumber_30:
            right = right + 1
        if Player.answer_16 == Subsession.RandomNumber_31 + Subsession.RandomNumber_32:
            right = right + 1
        if Player.answer_17 == Subsession.RandomNumber_33 + Subsession.RandomNumber_34:
            right = right + 1
        if Player.answer_18 == Subsession.RandomNumber_35 + Subsession.RandomNumber_36:
            right = right + 1
        if Player.answer_19 == Subsession.RandomNumber_37 + Subsession.RandomNumber_38:
            right = right + 1
        if Player.answer_20 == Subsession.RandomNumber_39 + Subsession.RandomNumber_40:
            right = right + 1
        Player.right1 = right
        Player.point1 = 2 * right

        # print("增加point!")
        if Player.id_in_subsession == Player.session.vars['r1'] or Player.id_in_subsession == Player.session.vars[
            'r2'] or Player.id_in_subsession == Player.session.vars['r3'] or Player.id_in_subsession == \
                Player.session.vars['r4'] or Player.id_in_subsession == Player.session.vars[
            'r5'] or Player.id_in_subsession == Player.session.vars['r6']:
            self.session.vars['A_CalculateGamePoint'] = self.session.vars['A_CalculateGamePoint'] + 2 * right

        if Player.id_in_subsession == Player.session.vars['b1'] or Player.id_in_subsession == Player.session.vars[
            'b2'] or Player.id_in_subsession == Player.session.vars['b3'] or Player.id_in_subsession == \
                Player.session.vars['b4'] or Player.id_in_subsession == Player.session.vars[
            'b5'] or Player.id_in_subsession == Player.session.vars['b6']:
            self.session.vars['B_CalculateGamePoint'] = self.session.vars['B_CalculateGamePoint'] + 2 * right

        if Player.id_in_subsession == Player.session.vars['g1'] or Player.id_in_subsession == Player.session.vars[
            'g2'] or Player.id_in_subsession == Player.session.vars['g3'] or Player.id_in_subsession == \
                Player.session.vars['g4'] or Player.id_in_subsession == Player.session.vars[
            'g5'] or Player.id_in_subsession == Player.session.vars['g6']:
            self.session.vars['C_CalculateGamePoint'] = self.session.vars['C_CalculateGamePoint'] + 2 * right


class CalculateGame2(Page):
    form_model = 'player'
    form_fields = ['answer_21', 'answer_22', 'answer_23', 'answer_24',
                   'answer_25', 'answer_26', 'answer_27', 'answer_28', 'answer_29', 'answer_30',
                   'answer_31', 'answer_32', 'answer_33', 'answer_34', 'answer_35', 'answer_36', 'answer_37',
                   'answer_38', 'answer_39', 'answer_40']

    # 1-12题 按照1-36随机数来依次计算
    # 顺序是横着数的 页面上第一列是1和2 第二列是3和4 以此类推
    def before_next_page(self):
        Subsession = self.subsession
        Group = self.group
        Player = self.player

        # 如果提交为空 则改为提交0即可

        if Player.answer_21 == None:
            Player.answer_21 = 0
        if Player.answer_22 == None:
            Player.answer_22 = 0
        if Player.answer_23 == None:
            Player.answer_23 = 0
        if Player.answer_24 == None:
            Player.answer_24 = 0
        if Player.answer_25 == None:
            Player.answer_25 = 0
        if Player.answer_26 == None:
            Player.answer_26 = 0
        if Player.answer_27 == None:
            Player.answer_27 = 0
        if Player.answer_28 == None:
            Player.answer_28 = 0
        if Player.answer_29 == None:
            Player.answer_29 = 0
        if Player.answer_30 == None:
            Player.answer_30 = 0
        if Player.answer_31 == None:
            Player.answer_31 = 0
        if Player.answer_32 == None:
            Player.answer_32 = 0
        if Player.answer_33 == None:
            Player.answer_33 = 0
        if Player.answer_34 == None:
            Player.answer_34 = 0
        if Player.answer_35 == None:
            Player.answer_35 = 0
        if Player.answer_36 == None:
            Player.answer_36 = 0
        if Player.answer_37 == None:
            Player.answer_37 = 0
        if Player.answer_38 == None:
            Player.answer_38 = 0
        if Player.answer_39 == None:
            Player.answer_39 = 0
        if Player.answer_40 == None:
            Player.answer_40 = 0

        # 计算正确个数 并加分
        right = 0

        if Player.answer_21 == Subsession.RandomNumber_41 + Subsession.RandomNumber_42:
            right = right + 1
        if Player.answer_22 == Subsession.RandomNumber_43 + Subsession.RandomNumber_44:
            right = right + 1
        if Player.answer_23 == Subsession.RandomNumber_45 + Subsession.RandomNumber_46:
            right = right + 1
        if Player.answer_24 == Subsession.RandomNumber_47 + Subsession.RandomNumber_48:
            right = right + 1
        if Player.answer_15 == Subsession.RandomNumber_49 + Subsession.RandomNumber_50:
            right = right + 1
        if Player.answer_26 == Subsession.RandomNumber_51 + Subsession.RandomNumber_52:
            right = right + 1
        if Player.answer_27 == Subsession.RandomNumber_53 + Subsession.RandomNumber_54:
            right = right + 1
        if Player.answer_28 == Subsession.RandomNumber_55 + Subsession.RandomNumber_56:
            right = right + 1
        if Player.answer_29 == Subsession.RandomNumber_57 + Subsession.RandomNumber_58:
            right = right + 1
        if Player.answer_30 == Subsession.RandomNumber_59 + Subsession.RandomNumber_60:
            right = right + 1
        if Player.answer_31 == Subsession.RandomNumber_61 + Subsession.RandomNumber_62:
            right = right + 1
        if Player.answer_32 == Subsession.RandomNumber_63 + Subsession.RandomNumber_64:
            right = right + 1
        if Player.answer_33 == Subsession.RandomNumber_65 + Subsession.RandomNumber_66:
            right = right + 1
        if Player.answer_34 == Subsession.RandomNumber_67 + Subsession.RandomNumber_68:
            right = right + 1
        if Player.answer_35 == Subsession.RandomNumber_69 + Subsession.RandomNumber_70:
            right = right + 1
        if Player.answer_36 == Subsession.RandomNumber_71 + Subsession.RandomNumber_72:
            right = right + 1
        if Player.answer_37 == Subsession.RandomNumber_73 + Subsession.RandomNumber_74:
            right = right + 1
        if Player.answer_38 == Subsession.RandomNumber_75 + Subsession.RandomNumber_76:
            right = right + 1
        if Player.answer_39 == Subsession.RandomNumber_77 + Subsession.RandomNumber_78:
            right = right + 1
        if Player.answer_40 == Subsession.RandomNumber_79 + Subsession.RandomNumber_80:
            right = right + 1
        Player.right2 = right
        Player.point2 = 2 * right

        # print("增加point!")
        if Player.id_in_subsession == Player.session.vars['r1'] or Player.id_in_subsession == Player.session.vars[
            'r2'] or Player.id_in_subsession == Player.session.vars['r3'] or Player.id_in_subsession == \
                Player.session.vars['r4'] or Player.id_in_subsession == Player.session.vars[
            'r5'] or Player.id_in_subsession == Player.session.vars['r6']:
            self.session.vars['A_CalculateGamePoint'] = self.session.vars['A_CalculateGamePoint'] + 2 * right
        if Player.id_in_subsession == Player.session.vars['b1'] or Player.id_in_subsession == Player.session.vars[
            'b2'] or Player.id_in_subsession == Player.session.vars['b3'] or Player.id_in_subsession == \
                Player.session.vars['b4'] or Player.id_in_subsession == Player.session.vars[
            'b5'] or Player.id_in_subsession == Player.session.vars['b6']:
            self.session.vars['B_CalculateGamePoint'] = self.session.vars['B_CalculateGamePoint'] + 2 * right
        if Player.id_in_subsession == Player.session.vars['g1'] or Player.id_in_subsession == Player.session.vars[
            'g2'] or Player.id_in_subsession == Player.session.vars['g3'] or Player.id_in_subsession == \
                Player.session.vars['g4'] or Player.id_in_subsession == Player.session.vars[
            'g5'] or Player.id_in_subsession == Player.session.vars['g6']:
            self.session.vars['C_CalculateGamePoint'] = self.session.vars['C_CalculateGamePoint'] + 2 * right


class Results(Page):
    timer_text = '倒计时结束自动跳转:'
    timeout_seconds = 40


page_sequence = [ResultsWaitPage1, SliderGameIntroduce, SliderGameTest, SliderGame, ResultsWaitPage2, Congratulations,
                 CalculateGameIntroduce, CalculateGame1, ResultsWaitPage3, ResultsAndCalculateGameIntroduce,
                 CalculateGame2, ResultsWaitPage4, Results, ]
# page_sequence = [ResultsWaitPage1 ]
# page_sequence = [ResultsWaitPage1,CalculateGame1]
